package com.leeh.jsontest;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * SQLiteOpenHelper:帮助类
 */
public class MySqliteHelper extends SQLiteOpenHelper {

    private static MySqliteHelper helper = null;
    private static SQLiteDatabase db = null;
    private static int openWriteCount = 0;

    /**
     * 继承了父类的构造函数
     *
     * @param context 上下文环境
     * @param name    数据库名称
     * @param factory 游标工厂
     * @param version 版本>=1
     */
    public MySqliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public static MySqliteHelper getInstance(Context context) {
        if (helper == null) {
            helper = new MySqliteHelper(context);
        }
        return helper;
    }

    /**
     * 自行创建的构造函数
     */
    private MySqliteHelper(Context context) {
        super(context, "infor.db", null, 1);
        //Log.i("执行构造函数", "///");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        List<String> sqlList = new ArrayList<String>();
        String sql1 = "create table teacher_course(term varchar(8),teacher_id varchar(32),teacher_name varchar(32),pattern varchar(8),html text)";
        String sql2 = "create table course_table(term varchar(32),course_id varchar(32),course_name varchar(32),pattern varchar(8),html text)";
        String sql3 = "create table classroom_table(term varchar(32),area varchar(32),teach_building varchar(32),classroom varchar(32),pattern varchar(8),html text)";
        String sql4 = "create table optional_table(term varchar(32),area varchar(32),html text)";

        String sql10 = "create table spinner_term(key varchar(32),value varchar(32))";
        String sql5 = "create table spinner_course(key varchar(32),value varchar(32))";
        String sql6 = "create table spinner_teacher(key varchar(32),value varchar(32))";
        String sql7 = "create table spinner_campus(key varchar(32),value varchar(32))";
        String sql8 = "create table spinner_building(key varchar(32),value varchar(32))";
        String sql9 = "create table spinner_classroom(key varchar(32),value varchar(32))";
        sqlList.add(sql1);
        sqlList.add(sql2);
        sqlList.add(sql3);
        sqlList.add(sql4);
        sqlList.add(sql5);
        sqlList.add(sql6);
        sqlList.add(sql7);
        sqlList.add(sql8);
        sqlList.add(sql9);
        sqlList.add(sql10);
        for (String sql : sqlList) {
            db.execSQL(sql);
        }
        //Log.i("创建数据库表", "///");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //Log.i("数据库升级", "///");
    }

    public void onOpen(SQLiteDatabase db) {
        super.onOpen(db);
        //Log.i("打开数据库", "///");
    }

    public synchronized SQLiteDatabase openDatabase() {
        if (openWriteCount == 0) {
            db = helper.getWritableDatabase();
        }
        openWriteCount++;
        return db;
    }

    public static synchronized void closeDatabase(SQLiteDatabase database) {
        openWriteCount--;
        if (openWriteCount == 0) {
            database.close();
        }
    }

}
